/**
* @describe 社交游戏socket类
* @author 游金宇(KM)
* @date 2024-09-12 11:41:27
*/


import { BattleInitialData, JoinBattleResponse, Player as BasePlayer, SocialGameClientOption, Event as SGCEvent, WatchBroadcastMessage, BattleInitialDataSchema, PlayerSchema, WatchBroadcastMessageSchema, ClientOption, RoomEvent, Room, RoomSchema, SeatSwapApplySchema, RoomKickSchema, IMMessageSchema, IMMessage, BattleCountdownStartSchema, AllEventResponsePairs } from 'sgc'
import { DescMessage, Message, toJson } from "@bufbuild/protobuf";
import { error, Game, game, log } from 'cc';

import { Event, Route } from `../../../pb-generate/server/[项目名]/[分支名]/v1/router_pb`;

import { cat, Manager, WrapperSocialGameClient } from '@shimotsuki/core';
import { ReflectMessage } from '@bufbuild/protobuf/reflect';

import { FinishedMiniSchema, GameBoutStartSchema, GameStartSchema, SelectLeadBroadcastSchema, ShowCardBroadcastSchema, SyncResponseSchema } from '../../../pb-generate/server/[项目名]/[分支名]/v1/msg_pb';

import { ReportGameStateRequest_GameSettlePayloadSchema, ReportGameStateRequestSchema } from '../../../pb-generate/server/[项目名]/[分支名]/v1/report_battle_pb';

import { DEBUG, WECHAT } from 'cc/env';

/**基础事件 */
export type BsseSocketEvent = keyof typeof SGCEvent;

/**业务事件 */
export type BusinessSocketEvent = keyof typeof Event;

/**异步路由 */
export type ASYNCSocketRoute = keyof typeof Route;

/**路由 */
export type SocketRoute = keyof typeof Route;

export type SocketEvent = BusinessSocketEvent | BsseSocketEvent | ASYNCSocketRoute | RoomEvent

// 事件响应类型
type EventResponsePair<T extends SocketEvent> = { event: T, responseType: DescMessage, cb?: (data: Message) => void };


declare module '@shimotsuki/core' {
    interface Manager {
        room_ws: WrapperSocialGameClient;
    }
}

/**房间事件对 */
const RoomEventResponsePairs: EventResponsePair<RoomEvent>[] = [
    /**房态变更 协议名称：Room.Info，返回参数：Room  */
    {
        event: RoomEvent.RoomInfo, responseType: RoomSchema, cb: (data) => {
            cat.store.room.roomInfo = data as Room
            log('----------------------房间事件对 roomInfo', cat.store.room.roomInfo)
        }
    },
    /**交换座位申请 协议名称： Room.SeatSwapApply，返回参数：SeatSwapApply*/
    { event: RoomEvent.RoomSeatSwapApply, responseType: SeatSwapApplySchema },
    /**房间踢出 协议名称： Room.Kick，返回参数：RoomKick*/
    { event: RoomEvent.RoomKick, responseType: RoomKickSchema },
    /**房内聊天消息 协议名称： Room.Kick，返回参数：IMMessage*/
    {
        event: RoomEvent.RoomMessage, responseType: IMMessageSchema, cb: (data) => {
            // const nonce = toJson(IMMessageSchema, data as IMMessage)
            // window.MINI_GAME_SDK.chat.send({ data: nonce });
        }
    },
    {
        event: RoomEvent.RoomBattleCountdownStart, responseType: BattleCountdownStartSchema
    }
]


export const CreateRoomWebSocket = () => {
    log('创建room ws')
    return cat.room_ws = cat.socialGameClient.createWebSocket(
        'room',
        {
            url: cat.env.room_ws_url,
            debug: DEBUG,
            isAutoConnect: true,
            isArrayBuffer: WECHAT,
        },
        {
            token: cat.store.login.room_token
        },
        {
            normalEvents: new Map(RoomEventResponsePairs.map(({ event, responseType, cb }) => [event, { event, responseType, cb }])),
        }
    )
}

